
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JComponent;
import javax.swing.JFrame;


public class GameFrame extends JFrame implements KeyListener{
	
	private static final long serialVersionUID = -8513762685417283338L;
	private MyComponent component;
	private int pointer = 2;
	private Gui data;
	GameFrame(Gui blubb)  {
		super();
		data = blubb;
		component = new MyComponent(blubb.getN(),
							blubb.getStart(),
							blubb.getGoal(),
							blubb.getHelp(),
							blubb.getGameLogic());
		System.out.println(blubb.getStart());
		/* THIS */ 	
		this.add(component);		
		this.setVisible(true);
		this.setSize(500, 400);
		this.setLocation(400, 200);
		this.setMinimumSize(new Dimension(data.getN()*65,data.getN()*40));
		
		
		/* KEY LISTENER */
		
		component.addKeyListener(this);
		this.addKeyListener(this);
				
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				System.exit(0);
			}
		});
		component.paint(component.getGraphics());
	}


	@Override
	public void keyPressed(KeyEvent event) {
		if(event.getKeyCode() == KeyEvent.VK_LEFT) 
			if (pointer > 1){
				pointer --;
				component.setPointer(pointer);
			}
		if(event.getKeyCode() == KeyEvent.VK_RIGHT) 
			if (pointer < 3){
				pointer ++;
				component.setPointer(pointer);
			}
		if(event.getKeyChar()== KeyEvent.VK_SPACE){
			if (!data.getGameLogic().getPicked()) {
				if (pointer == data.getStart()){ 
					data.getGameLogic().pick(data.getGameLogic().getStart());
					component.pickDisk(data.getStart(),component.getGraphics());
				}
				if (pointer == data.getGoal()){
					data.getGameLogic().pick(data.getGameLogic().getGoal());
					component.pickDisk(data.getGoal(),component.getGraphics());
				}
				if (pointer == data.getHelp()){
					data.getGameLogic().pick(data.getGameLogic().getHelp());
					component.pickDisk(data.getHelp(),component.getGraphics());
				}
			}
			else {
				if(pointer == data.getStart())
					if(data.getGameLogic().pull(data.getGameLogic().getStart()))
						component.drawDisk(data.getGameLogic().getCurrent().getD().getSize(), 
								data.getStart(),
								component.getGraphics());
				if(pointer == data.getGoal())
					if (data.getGameLogic().pull(data.getGameLogic().getGoal()))
						component.drawDisk(data.getGameLogic().getCurrent().getD().getSize(),
								data.getGoal(),
								component.getGraphics());
				if(pointer == data.getHelp())
					if(data.getGameLogic().pull(data.getGameLogic().getHelp()))
						component.drawDisk(data.getGameLogic().getCurrent().getD().getSize(),
								data.getHelp(),
								component.getGraphics());
				}
			data.refresh();
			}
				
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	public JComponent getComponent(){
		return component;
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
}
	



